package personal.lijun.skeleton.springcloud.dataaccess.druid;

import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.orderbyhelper.OrderByHelper;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @Auther: lijun@sstcsoft.com
 * @Date: 2019/9/3 17:32
 */
@Configuration
@MapperScan(basePackages = {"personal.lijun.skeleton.springcloud.dataaccess.dao"})
public class DruidDataSourceInitPropertiesConfig {

    @Autowired
    private DruidDataSourceProperties druidDataSourceProperties;

    @Bean
    @ConditionalOnMissingBean(DruidDataSourceProperties.class)
    @ConditionalOnProperty(value = "spring.datasource.type",havingValue = "com.alibaba.druid.pool.DruidDataSource")
    public DruidDataSourceProperties druidDataSourceProperties(){
        DruidDataSourceProperties druidDataSourceProperties = new DruidDataSourceProperties();
        druidDataSourceProperties.setValidationQuery("");
        return new DruidDataSourceProperties();
    }

    @Bean
    @ConditionalOnClass(Slf4jLogFilter.class)
    @ConditionalOnProperty(value = "spring.datasource.type",havingValue = "com.alibaba.druid.pool.DruidDataSource")
    public Slf4jLogFilter slf4jLogFilter(){
        Slf4jLogFilter filter = new Slf4jLogFilter();
        filter.setStatementExecutableSqlLogEnable(false);
        return filter;
    }

    @Bean
    @ConditionalOnClass(DruidDataSource.class)
    @ConditionalOnProperty(value = "spring.datasource.type",havingValue = "com.alibaba.druid.pool.DruidDataSource")
    public  DruidDataSource initDataSource(DruidDataSourceProperties druidDataSourceProperties) throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(druidDataSourceProperties.getUrl());
        druidDataSource.setUsername(druidDataSourceProperties.getUsername());
        druidDataSource.setPassword(druidDataSourceProperties.getPassword());

        druidDataSource.getProxyFilters().add(slf4jLogFilter());
        if(druidDataSourceProperties.getInitialSize()!=null)
            druidDataSource.setInitialSize(druidDataSourceProperties.getInitialSize());
        if(druidDataSourceProperties.getMinIdle()!=null){
            druidDataSource.setMinIdle(druidDataSourceProperties.getMinIdle());
        }
        if(druidDataSourceProperties.getMaxActive()!=null){
            druidDataSource.setMaxActive(druidDataSourceProperties.getMaxActive());
        }
        if(druidDataSourceProperties.getMaxWait()!=null){
            druidDataSource.setMaxWait(druidDataSourceProperties.getMaxWait());
        }
        if(druidDataSourceProperties.getTimeBetweenEvictionRunsMillis()!=null){
            druidDataSource.setTimeBetweenEvictionRunsMillis(druidDataSourceProperties.getTimeBetweenEvictionRunsMillis());
        }
        if(druidDataSourceProperties.getMinEvictableIdleTimeMillis()!=null){
            druidDataSource.setMinEvictableIdleTimeMillis(druidDataSourceProperties.getMinEvictableIdleTimeMillis());
        }
        if(druidDataSourceProperties.getTestWhileIdle()!=null){
            druidDataSource.setTestWhileIdle(druidDataSourceProperties.getTestWhileIdle());
        }
        if(druidDataSourceProperties.getTestOnBorrow()!=null){
            druidDataSource.setTestOnBorrow(druidDataSourceProperties.getTestOnBorrow());
        }
        if(druidDataSourceProperties.getTestOnReturn()!=null){
            druidDataSource.setTestOnReturn(druidDataSourceProperties.getTestOnReturn());
        }
        if(druidDataSourceProperties.getPoolPreparedStatements()!=null){
            druidDataSourceProperties.setPoolPreparedStatements(druidDataSourceProperties.getPoolPreparedStatements());
        }
        if(druidDataSourceProperties.getMaxPoolPreparedStatementPerConnectionSize()!=null){
            druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(druidDataSourceProperties.getMaxPoolPreparedStatementPerConnectionSize());
        }
        if(druidDataSourceProperties.getDefaultAutoCommit()!=null){
            druidDataSource.setDefaultAutoCommit(druidDataSourceProperties.getDefaultAutoCommit());
        }
        if(druidDataSourceProperties.getValidationQuery()!=null){
            druidDataSource.setValidationQuery(druidDataSourceProperties.getValidationQuery());
        }
        if(druidDataSourceProperties.getFilters()!=null){
            druidDataSource.setFilters(druidDataSourceProperties.getFilters());
        }
        return druidDataSource;
    }

    @Bean
    @ConditionalOnClass(OrderByHelper.class)
    public OrderByHelper sqlSessionFactoryBean(DataSource dataSource){
        OrderByHelper orderByHelper = new OrderByHelper();
        return orderByHelper;
    }
}
